package user

import (
	"zhou_gin_api/app/dao"
	"zhou_gin_api/app/input"
	"zhou_gin_api/app/models"
	"zhou_gin_api/app/output/code"
)

type userCommandService struct {
}

func NewUserCommandService() *userCommandService {
	return &userCommandService{}
}

func (service *userCommandService) Add(addInput input.AddUserInput) (id uint, err error) {
	userDao := dao.NewUserDao()

	var userModel models.UserModel
	userModel.Username = addInput.Username
	userModel.Password = addInput.Password
	userModel.Age = addInput.Age

	return userDao.Add(&userModel)
}

func (service *userCommandService) Update(updateInput input.UpdateUserInput) (errCode string, msg string) {
	userDao := dao.NewUserDao()

	userModel, err := userDao.First(updateInput.Id)
	if err != nil {
		return code.ResponseCode["DATA_NOT_EXIST"], ""
	}

	userModel.Username = updateInput.Username
	userModel.Password = updateInput.Password
	userModel.Age = updateInput.Age

	err = userDao.Update(userModel)

	if err != nil {
		return code.ResponseCode["EXCEPTION_MESSAGE"], err.Error()
	}

	return code.ResponseCode["SUCCESS"], ""
}

func (service *userCommandService) Delete(deleteInput input.DeleteUserInput) (errCode string, msg string) {
	userDao := dao.NewUserDao()

	userModel, err := userDao.First(deleteInput.Id)
	if err != nil {
		return code.ResponseCode["DATA_NOT_EXIST"], ""
	}

	err = userDao.Delete(userModel)
	if err != nil {
		return code.ResponseCode["EXCEPTION_MESSAGE"], err.Error()
	}

	return code.ResponseCode["SUCCESS"], ""
}
